<template>
  <div class="user-index">
    <div class="toolbar">
      <el-button icon="el-icon-refresh" @click="refresh" :loading="loading"></el-button>
      <el-button type="danger" @click="add">添加</el-button>
    </div>

    <el-table border v-loading="loading" :data="data">
      <el-table-column label="账号" prop="username"></el-table-column>
      <el-table-column label="昵称" prop="nickname"></el-table-column>
      <el-table-column label="电话" prop="phone"></el-table-column>
      <el-table-column label="邮箱" prop="email"></el-table-column>
      <el-table-column label="头像" prop="avatar" width="70">
        <template slot-scope="scope">
          <el-avatar :src="resourceUrl + scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="最后一次登录时间" prop="last_login_time" width="160">
        <template slot-scope="scope">
          <span>{{scope.row.last_login_time | moment('YYYY-MM-DD HH:mm:ss')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后一次登录IP" prop="last_login_ip"></el-table-column>
      <el-table-column label="状态" prop="status" width="70">
        <template slot-scope="scope">
          <el-tag>{{scope.row.status | userStatus}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="160">
        <template slot-scope="{row}">
          <el-button size="mini" type="primary" :disabled="!!row.is_system" @click="edit(row)">编辑</el-button>
          <el-button size="mini" type="danger" :disabled="!!row.is_system" @click="del(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :hide-on-single-page="true"
      :total="pageTotal"
      :pageSize="pageSize"
      layout="prev, pager, next, jumper, ->, total"
      @current-change="changePage"
    ></el-pagination>

    <el-dialog
      width="600px"
      top="5vh"
      :title="ruleForm.id ? '修改' : '添加'"
      :visible.sync="showDialog"
    >
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="账号" prop="username">
          <el-input v-model="ruleForm.username" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            :placeholder="ruleForm.id ? '密码不修改请置空' : '请输入密码'"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email" placeholder="fsyzxz@163.com"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="ruleForm.nickname" placeholder="请输入昵称"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="ruleForm.phone" placeholder="183********"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="status">
          <el-switch v-model="ruleForm.status" :active-value="1" :inactive-value="0" :label="1"></el-switch>
        </el-form-item>
        <el-form-item label="角色" prop="role_id">
          <el-select v-model="ruleForm.role_id" placeholder="请选择所属角色">
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button type="primary" size="small" @click="submit('ruleForm')">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import table from '../../../mixins/table'

export default {
  name: 'userIndex',
  mixins: [table],
  data () {
    return {
      url: '/admin/users',
      delUrl: '/admin/user',
      editUrl: '/admin/user',
      addUrl: '/admin/user',
      options: [],
      rules: {
        username: [{ required: true, message: '请输入登录账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入登录密码', trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
        role_id: [{ required: true, message: '请选择所属角色', trigger: 'blur' }]
      }
    }
  },
  mounted () {
    this.loadData()

    this.loadRoles()
  }
}
</script>
